const gulp = require('gulp');
const cssmin = require('gulp-cssmin');
const autoPrefixer = require('gulp-autoprefixer');
const sass = require('gulp-sass')(require('sass'));
const uglify = require('gulp-uglify');
const babel = require('gulp-babel');
const htmlmin = require('gulp-htmlmin');
const clean = require('gulp-clean');
const runSequence = require('run-sequence');
const webserver = require('gulp-webserver');
const cssnano = require('gulp-cssnano');
gulp.task('css',gulp.series(()=>{
    return gulp
    .src('./src/css/**') //找到哪些文件进行操作
    .pipe(autoPrefixer())
    .pipe(cssmin()) //指要做什么
    .pipe(gulp.dest('./dist/css')) //是指输出到哪个目录下,如果没有这个目录存在会自动创建这个目录
}))

gulp.task('sass',gulp.series(()=>{
    return gulp
    .src('./src/sass/**')
    .pipe(sass())
    .pipe(autoPrefixer())
    .pipe(cssnano({
        zindex:false,
        autoPrefixer:false,
        discardComments:{removeAll:true},
        svg:false
    }))
    .pipe(cssmin())
    .pipe(gulp.dest('./dist/css'))
}))


gulp.task('js',gulp.series(()=>{
    return gulp
    .src('./src/js/**')
    .pipe(babel({
        presets:['es2015']
    }))
    .pipe(uglify())
    .pipe(gulp.dest('./dist/js'))
}))

gulp.task('html',gulp.series(()=>{
    return gulp
    .src('./src/pages/**')
    .pipe(htmlmin({
        removeEmptyAttributes:true,
        collapseWhitespace:true
    }))
    .pipe(gulp.dest('./dist/pages'))
}))

gulp.task('static',gulp.series(()=>{
    return gulp
    .src('./src/static/**')
    .pipe(gulp.dest('./dist/static'))
}))

gulp.task('clean',gulp.series(()=>{
    return gulp
    .src('/dist',{allowEmpty: true})
    .pipe(clean())
}))

gulp.task('default',gulp.series('clean','css','sass','js','html','static'));

// gulp.task('default',gulp.series(()=>{
//     runSequence('clean',['css','sass','js','html','static'])
// }));

gulp.task('webserver',function(){
    return gulp
    .src('./dist')
    .pipe(webserver({
        host:'localhost',
        port:3000,
        livereload:true,
        open:"./pages/index.html"
    }))
})

// 10. 创建一个 watch 任务
// gulp.task('watch', function () {
//     gulp.watch('./src/css/**', ['css'])
//     gulp.watch('./src/sass/**', ['sass'])
//     gulp.watch('./src/js/**', ['js'])
//     gulp.watch('./src/pages/**', ['html'])
//     gulp.watch('./src/static/**', ['static'])
// })

// gulp.task("watch",async()=>{
//     gulp.watch('./src/css/**',async()=>{
//         gulp.src('./src/css/**',async()=>{
        
//         })
//     })
// })
